<template>
  <div class="login-container">

    <!--    <div class="loginheader">-->

    <!--      <div style="display: flex;flex-direction: row; margin-left: 2%;">-->
    <!--        <img src="../../assets/images/logo.png" alt="" class="logoImgHeader" /><br><br>-->
    <!--      </div>-->
    <!--    </div>-->


    <div
      style="width: 1000px;background-color: #FFFFFF;
      filter:progid:DXImageTransform.Microsoft.Shadow(color=#909090,direction=120,strength=4);

   -moz-box-shadow: 4px 4px 20px #909090;

   -webkit-box-shadow: 4px 4px 20px #909090;

   box-shadow:4px 4px 20px #909090;
      border: 0px solid #999999;margin-left: 24%;margin-top: 10%;display: flex;flex-direction: row;border-radius: 10px;opacity: 0.9">

      <div style="width: 49%;text-align: center">
        <div style="display: flex;flex-direction: row;margin-top: 35%;margin-left: 40%">
          <img src="../../assets/images/logo.png" alt="" class="logoImgHeader"/><br><br>

        </div>
        <br><br>
        <div style="display: flex;flex-direction: row;margin-left: 26%;font-size: 24px;font-weight: 700">
          <span class="title">言川企业协同办公平台</span>
        </div>

        <div style="display: flex;flex-direction: row;font-size: 12px;text-align: center;margin-left: 28%;margin-top: 25%">
          <div style="margin-top: 2%">
            <span>上海市鸣冉电子科技有限公司@(版权所有)</span>
            <br/><br/>
            <span>一言九鼎,川流不息</span>
          </div>
        </div>
        <div>

        </div>
      </div>
      <div style="width: 50%;padding: 20px">


        <el-tabs v-model="activeTab" @tab-click="handleClick" style="font-size: 20px">
          <el-tab-pane label="账号密码登录" name="1" style="font-size: 16px">
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on"
                     label-position="left">

              <div style="border: 4px solid rgb(122, 164, 209);padding: 20px;width: 100%;height: 100%">
                <div class="title-container">
                  <!--            <img src="../../assets/images/logo.png" alt="" class="logoImg" /><br><br>-->
                  <el-button

                    type="primary" v-if="registerRouter" @click="registerRouters">路由注册
                  </el-button>
                </div>
                <!--              v-if="registerRouter" -->
                <el-form-item prop="username" style="width: 80%;margin-left: 10%">
        <span class="svg-container">
          <svg-icon icon-class="user" style="color: #1e88f7"/>
        </span>
                  <el-input
                    ref="username"
                    v-model="loginForm.username"
                    placeholder="用户名"
                    name="username"
                    type="text"
                    tabindex="1"
                    autocomplete="on"
                  />
                </el-form-item>

                <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
                  <el-form-item prop="password" style="width: 80%;margin-left: 10%">
          <span class="svg-container">
            <svg-icon icon-class="password" style="color: #1e88f7"/>
          </span>
                    <el-input
                      :key="passwordType"
                      ref="password"
                      v-model="loginForm.password"
                      :type="passwordType"
                      placeholder="密码"
                      name="password"
                      tabindex="2"
                      autocomplete="on"
                      @keyup.native="checkCapslock"
                      @blur="capsTooltip = false"
                      @keyup.enter.native="handleLogin"
                    />
                    <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" style="color: #1e88f7"/>
          </span>
                  </el-form-item>
                </el-tooltip>

                <el-form-item prop="checkCode"
                              style="display: inline-block; width: 180px;font-size: 12px;margin-left: 10%">
        <span class="svg-container">
          <svg-icon icon-class="checkCode"/>
        </span>
                  <el-input
                    ref="checkCode"
                    v-model="loginForm.checkCodeValue"
                    placeholder="验证码"
                    name="checkCode"
                    type="text"
                    tabindex="4"
                  />
                </el-form-item>
                <img id="verImg" class="checkCodeImg" :src="checkCodeBase64" width="130px" height="48px"
                     @click="getCheckCode"/>
                <el-button :loading="loading" type="primary" style="width:80%;margin-bottom:30px;margin-left: 10%"
                           @click.native.prevent="handleLogin">登录
                </el-button>
              </div>

            </el-form>

          </el-tab-pane>
          <el-tab-pane label="短信验证登录" name="2" style="font-size: 16px">


            <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on"
                     label-position="left">

              <div style="border: 4px solid rgb(122, 164, 209);padding: 20px;width: 100%;height: 100%">
                <div class="title-container">
                  <!--            <img src="../../assets/images/logo.png" alt="" class="logoImg" /><br><br>-->

                  <el-button

                    type="primary" v-if="registerRouter" @click="registerRouters">路由注册
                  </el-button>
                </div>
                <!--              v-if="registerRouter" -->
                <el-form-item prop="username" style="width: 80%;margin-left: 10%">
        <span class="svg-container">
          <svg-icon icon-class="user" style="color: #1e88f7"/>
        </span>
                  <el-input
                    ref="username"
                    v-model="loginForm.username"
                    placeholder="用户名"
                    name="username"
                    type="text"
                    tabindex="1"
                    autocomplete="on"
                  />
                </el-form-item>

                <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
                  <el-form-item prop="password" style="width: 80%;margin-left: 10%">
          <span class="svg-container">
            <svg-icon icon-class="password" style="color: #1e88f7"/>
          </span>
                    <el-input
                      :key="passwordType"
                      ref="password"
                      v-model="loginForm.password"
                      :type="passwordType"
                      placeholder="密码"
                      name="password"
                      tabindex="2"
                      autocomplete="on"
                      @keyup.native="checkCapslock"
                      @blur="capsTooltip = false"
                      @keyup.enter.native="handleLogin"
                    />
                    <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" style="color: #1e88f7"/>
          </span>
                  </el-form-item>
                </el-tooltip>

                <el-form-item prop="checkCode"
                              style="display: inline-block; width: 180px;font-size: 12px;margin-left: 10%">
        <span class="svg-container">
          <svg-icon icon-class="checkCode"/>
        </span>
                  <el-input
                    ref="checkCode"
                    v-model="loginForm.checkCodeValue"
                    placeholder="验证码"
                    name="checkCode"
                    type="text"
                    tabindex="4"
                  />
                </el-form-item>
                <img id="verImg2" class="checkCodeImg" :src="checkCodeBase64" width="130px" height="48px"
                     @click="getCheckCode"/>
                <el-button :loading="loading" type="primary" style="width:80%;margin-bottom:30px;margin-left: 10%"
                           @click.native.prevent="handleLogin">登录
                </el-button>
              </div>

            </el-form>


          </el-tab-pane>
          <el-tab-pane label="微信扫码登录" name="3" style="font-size: 16px">

            <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on"
                     label-position="left">

              <div style="border: 4px solid rgb(122, 164, 209);padding: 20px;width: 100%;height: 100%">
                <div class="title-container">
                  <!--            <img src="../../assets/images/logo.png" alt="" class="logoImg" /><br><br>-->
                  <span class="title">言川企业协同办公平台</span>
                  <el-button

                    type="primary" v-if="registerRouter" @click="registerRouters">路由注册
                  </el-button>
                </div>
                <!--              v-if="registerRouter" -->
                <el-form-item prop="username" style="width: 80%;margin-left: 10%">
        <span class="svg-container">
          <svg-icon icon-class="user" style="color: #1e88f7"/>
        </span>
                  <el-input
                    ref="username"
                    v-model="loginForm.username"
                    placeholder="用户名"
                    name="username"
                    type="text"
                    tabindex="1"
                    autocomplete="on"
                  />
                </el-form-item>

                <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
                  <el-form-item prop="password" style="width: 80%;margin-left: 10%">
          <span class="svg-container">
            <svg-icon icon-class="password" style="color: #1e88f7"/>
          </span>
                    <el-input
                      :key="passwordType"
                      ref="password"
                      v-model="loginForm.password"
                      :type="passwordType"
                      placeholder="密码"
                      name="password"
                      tabindex="2"
                      autocomplete="on"
                      @keyup.native="checkCapslock"
                      @blur="capsTooltip = false"
                      @keyup.enter.native="handleLogin"
                    />
                    <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" style="color: #1e88f7"/>
          </span>
                  </el-form-item>
                </el-tooltip>

                <el-form-item prop="checkCode"
                              style="display: inline-block; width: 180px;font-size: 12px;margin-left: 10%">
        <span class="svg-container">
          <svg-icon icon-class="checkCode"/>
        </span>
                  <el-input
                    ref="checkCode"
                    v-model="loginForm.checkCodeValue"
                    placeholder="验证码"
                    name="checkCode"
                    type="text"
                    tabindex="4"
                  />
                </el-form-item>
                <img id="verImg3" class="checkCodeImg" :src="checkCodeBase64" width="130px" height="48px"
                     @click="getCheckCode"/>
                <el-button :loading="loading" type="primary" style="width:80%;margin-bottom:30px;margin-left: 10%"
                           @click.native.prevent="handleLogin">登录
                </el-button>
              </div>

            </el-form>


          </el-tab-pane>

        </el-tabs>

      </div>


    </div>

    <!--    <div class="loginFooter">-->

    <!--      <div style="margin-top: 2%">-->
    <!--        <span>上海市鸣冉电子科技有限公司@(版权所有)</span>-->
    <!--        <br /><br />-->
    <!--        <span>一言九鼎,川流不息</span>-->
    <!--      </div>-->
    <!--    </div>-->

  </div>
</template>

<script>
  import { asyncRoutes } from '@/router'
  import * as apiRouter from '@/api/router'
  import * as routerHelper from '@/utils/routerHelper'
  import * as apiUser from '@/api/user'
  import * as userinfo from '@/api/userinfo'

  export default {
    name: 'Login',
    // components: { SocialSign },
    computed: {
      registerRouter() {
        return this.$route.query.registerRouter
      }
    },
    data() {
      const validatePassword = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error('The password can not be less than 6 digits'))
        } else {
          callback()
        }
      }
      return {
        loginForm: {
          username: '',
          password: '',
          checkCodeKey: undefined,
          checkCodeValue: undefined,
          appName: process.env.VUE_APP_APP_NAME
        },
        loginRules: {
          username: [{ required: true, trigger: 'blur' }],
          password: [{ required: true, trigger: 'blur', validator: validatePassword }]
        },
        passwordType: 'password',
        capsTooltip: false,
        loading: false,
        redirect: undefined,
        otherQuery: {},
        checkCodeBase64: undefined,
        person: {},
        activeTab: '1'
      }
    },
    watch: {
      $route: {
        handler: function(route) {
          const query = route.query
          if (query) {
            this.redirect = query.redirect
            this.otherQuery = this.getOtherQuery(query)
          }
        },
        immediate: true
      }
    },
    created() {
      // window.addEventListener('storage', this.afterQRScan)
      apiUser.getCheckCode().then((res) => {
        this.checkCodeBase64 = res.content.image
        this.loginForm.checkCodeKey = res.content.key
      })
    },
    mounted() {
      window.addEventListener('keydown', this.keyDown)
      if (this.loginForm.username === '') {
        this.$refs.username.focus()
      } else if (this.loginForm.password === '') {
        this.$refs.password.focus()
      }
    },
    destroyed() {
      // window.removeEventListener('storage', this.afterQRScan)
      window.removeEventListener('keydown', this.keyDown, false)
    },
    methods: {

      keyDown(e) {
        // 回车则执行登录方法 enter键的ASCII是13
        if (e.keyCode === 13) {
          this.handleLogin() // 定义的登录方法
        }
      },

      checkCapslock({ shiftKey, key } = {}) {
        if (key && key.length === 1) {
          if (shiftKey && (key >= 'a' && key <= 'z') || !shiftKey && (key >= 'A' && key <= 'Z')) {
            this.capsTooltip = true
          } else {
            this.capsTooltip = false
          }
        }
        if (key === 'CapsLock' && this.capsTooltip === true) {
          this.capsTooltip = false
        }
      },
      showPwd() {
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
      async handleLogin() {

        console.log('后端api地址: ', process.env.VUE_APP_BASE_API)
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('user/loginWithCheckCode', this.loginForm)
              .then(() => {
                this.$store.dispatch('user/getInfo')
                this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
                this.loading = false
                userinfo.loginSave().then(() => {

                })
              })
              .catch(() => {
                this.loading = false
                // 登录失败,刷新验证码
                apiUser.getCheckCode().then((res) => {
                  this.checkCodeBase64 = res.content.image
                  this.loginForm.checkCodeKey = res.content.key
                })
              })
          } else {
            console.log('error submit!!')
            return false
          }
        })

      },
      getOtherQuery(query) {
        return Object.keys(query).reduce((acc, cur) => {
          if (cur !== 'redirect') {
            acc[cur] = query[cur]
          }
          return acc
        }, {})
      },
      registerRouters() { //项目路由注册到union auth
        // 获取项目 asyncRoutes路由
        console.log('asyncRoutes--->', asyncRoutes)
        // 对asyncRoutes进行简化处理
        let asyncRoutesAfter = routerHelper.generateRouterTree(asyncRoutes)
        let appName = process.env.VUE_APP_APP_NAME
        apiRouter.registerRouter({ 'router': JSON.stringify(asyncRoutesAfter), appName }).then(
          result => {
            this.$message({
              message: '项目路由注册成功',
              type: 'success'
            })
          }
        ).catch(
          error => {
            this.$message({
              message: '路由注册失败' + error.message(),
              type: 'warning'
            })
          }
        )
      },
      async getCheckCode() {
        const { content } = await apiUser.getCheckCode()
        this.checkCodeBase64 = content.image
        this.loginForm.checkCodeKey = content.key
      },
      handleClick(tab, event) {
        if (tab.name == '1') {
          this.selectType = 1
        } else {

          this.selectType = 2

        }
      }
    }
  }
</script>

<style lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg: #E0E0E0;
  $light_gray: #333333;
  $cursor: #333333;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }

  /* reset element-ui css */
  .login-container {

    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
  }
</style>

<style lang="scss" scoped>
  $bg: #333333;
  $dark_gray: #333333;
  $light_gray: #333333;

  .login-container {
    min-height: 100%;
    height: 100%;
    width: 100%;
    overflow: hidden;
    background-image: url("../../assets/images/bg.png");
    background-size: 100% 100%;
    background-repeat: no-repeat;
    background-color: #ffffff;

    .login-form {
      position: relative;
      width: 550px;
      height: 450px;
      max-width: 100%;
      overflow: hidden;
      padding: 10px;
      background-color: #FFFFFF;

      border-radius: 10px;
      /*margin-left: 55%;*/
      /*margin-top: 5%;*/
      /*opacity: 0.9;*/
    }

    .tips {
      font-size: 14px;
      color: #333333;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: #333333;
      vertical-align: middle;
      width: 10px;
      display: inline-block;
      font-size: 10px;
    }

    .title-container {
      position: relative;
      margin-bottom: 30px;
      text-align: center;
      margin-top: 12%;

      .title {
        font-size: 24px;
        color: #000000;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .thirdparty-button {
      position: absolute;
      right: 0;
      bottom: 6px;
    }

    @media only screen and (max-width: 470px) {
      .thirdparty-button {
        display: none;
      }
    }
  }

  .checkCodeImg {
    position: relative;
    top: 20px;
  }

  .loginFooter {
    width: 100%;
    position: absolute;
    bottom: 0%;
    font-size: 16px;
    text-align: center;
    color: #FFFFFF;
    font-family: 黑体;
    display: flex;
    justify-content: center;
    background-color: #1890ff;
    height: 12%;
    opacity: 0.7;
  }

  .loginheader {
    width: 100%;
    font-size: 20px;
    color: #000000;
    font-family: 黑体;
    background-color: #1890ff;
    height: 12%;
    opacity: 0.7;

  }

  .logoImg {
    width: 80px;
    height: auto;
    border-radius: 10px;
  }

  .logoImgHeader {
    width: 100px;
    height: auto;
    border-radius: 10px;
    margin-top: 0.5%;

  }
</style>



